/*
 * Created on Aug 12, 2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package neuro.uu.se.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class ThreadedForEachTest {

//	Skapar två ArrayLists med ArrayLists i. De innehåller slumptal för senare sortering. 
//	De två ArrayList är identiska för att kunna testa hur lång tid det tar att sortera dem 
//	trådat och icke trådat

	static private void initList(){
		System.out.println("Initializing lists");
		ArrayList temp, tempo, tempot,tempoto;
		int into;
		for (int i = 0; i < randomNolists ; i++) {
			temp = new ArrayList(length);
			tempo = new ArrayList(length);
			tempot = new ArrayList(length);
			tempoto = new ArrayList(length);
			for (int j = 0; j < length; j++) {
				into = Math.round((float)Math.random()*100000);
				temp.add(new Integer(into));
				tempo.add(new Integer(into));
				tempot.add(new Integer(into));
				tempoto.add(new Integer(into));
			}
			list.add(tempo);
			list2.add(tempot);
			list3.add(tempoto);
			singleCoreList.add(temp);
		}
	}
	
	static private void clear(){
		list = new ArrayList();
		singleCoreList = new ArrayList();
	}
	
	static private int randomNolists = 1;
	static private int length = 100000;
	static private ArrayList list = new ArrayList();
	static private ArrayList list2 = new ArrayList();
	static private ArrayList list3 = new ArrayList();
	static private ArrayList singleCoreList = new ArrayList();
	
	public static void main(String[] args) {

		initList();
		
		Utils.initThreadPoolExecutor();
		
		long time = System.nanoTime();
		
//		Den faktiska forEach metoden
		try {
			Utils.forEachListObject(new ObjectSortStrategy(), list2);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		time = System.nanoTime() - time;
		
		System.out.println("Threaded forEachIndividualy with init penalty: " +(float)time/1000000000 +"s");
		
		ArrayList results = new ArrayList();
		time = System.nanoTime();
		
//		Den faktiska forEach metoden
		try {
			results = Utils.forEachListObject(new ObjectSortStrategy(), list3);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		time = System.nanoTime() - time;
		
//		for (Iterator iter = results.iterator(); iter.hasNext();) {
//			int element = (Integer) iter.next();
//			System.out.println(+element);
//		}
		
		System.out.println("Threaded forEachIndividualy: " +(float)time/1000000000 +"s");
		
		time = System.nanoTime();
		
//		Den faktiska forEach metoden
		try {
			Utils.forEachDivideList(new CollectionSortStrategy(), list);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		time = System.nanoTime() - time;
		
		System.out.println("Threaded forEach: " +(float)time/1000000000 +"s");
				
		
		time = System.nanoTime();

		WorkStrategy sortStrategy = new CollectionSortStrategy();
		
		sortStrategy.perform(singleCoreList);

		time = System.nanoTime() - time;
		
		System.out.println("Unthreaded forEach: " +(float)time/1000000000 +"s");

//		Eftersom threadPoolExecutorn är igång hela tiden för kunna återanvändas måste man döda den explicit
		try {
			Utils.terminateThreadPoolExecutor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

}
